package me.yuxiaoyao.lock.zookeeper;

import me.yuxiaoyao.lock.LockExecutor;
import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;

import java.util.concurrent.locks.Lock;

/**
 * @author kerryzhang on 2021/01/05
 */

public class ZookeeperLockExecutor implements LockExecutor {
    private final ZookeeperLockRegistry zookeeperLockRegistry;

    public ZookeeperLockExecutor(ZookeeperLockRegistry zookeeperLockRegistry) {
        this.zookeeperLockRegistry = zookeeperLockRegistry;
    }

    @Override
    public Lock getLock(Object lockKey) {
        // current zookeeper key only support String type
        return zookeeperLockRegistry.obtain(lockKey.toString());
    }

    @Override
    public boolean isSupportFair() {
        return true;
    }
}
